import numpy as np
import random
import pymongo
import redis

# 全局数据
n_feature = 14
n_recommend = 5
prospect = 5
filter = 1


client = pymongo.MongoClient(host='localhost',port=27017)
db_mongo = client['recommender']
collection_user = db_mongo['user']
collenction_statistic = db_mongo['statistic']

pool = redis.ConnectionPool(host='127.0.0.1', db=5)
db_redis = redis.StrictRedis(connection_pool=pool, decode_responses=True)


def createUser(n:int):
    '''
    randomly create some users
    into mysql

    :param n: number of users
    :return:
    '''

    password = "123"  # 固定默认

    created_username = set()   # 已经用过的用户名,
    all_char = list("qazwsxedcrfvtgbyhnujmikolp")  # 所有字母

    for i in range(n):
        # 生成用户名
        while True:
            username_len = random.randint(1,10)    #用户名的长度是个随机数
            username = ''.join(random.sample(all_char, username_len))
            if username not in created_username:
                print(username)
                created_username.add(username)
                break


        # 生成爱好的二进制串
        hobby = np.random.randint(0,2,size=(n_feature,))

        friend_preference = (hobby*0.6 + 0.2).tolist()

        hobby = hobby.tolist()

        transition_preference = np.random.random((14, 2, 2)).tolist()

        collection_user.insert({'_id': username,
                                'password': password,
                                'history': [hobby],
                                'history_reward': [0.5],
                                'hobby': hobby,
                                'friend_preference': friend_preference,
                                'transition_preference': transition_preference})




if __name__ == "__main__":
    pass
    ### 删除redis里面的数据
    # for i in db_redis.keys():
    #     db_redis.delete(i)

    # print(db_redis.keys())
    # db_redis.sadd('thing','hj')
    # res=db_redis.sismember('thing','hj')
    # print(res)
    # print(db_redis.keys())
    # db_redis.lpush('time',1)

    # createUser(100)












